home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / aout-adobe.c < prev    next >
C/C++ Source or Header  |  1994-06-20  |  16KB  |  531 lines

  1. /* BFD back-end for a.out.adobe binaries.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.  Based on bout.c.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "libbfd.h"
  24.  
  25. #include "aout/adobe.h"
  26.  
  27. #include "aout/stab_gnu.h"
  28. #include "libaout.h"        /* BFD a.out internal data structures */
  29.  
  30. extern const bfd_target a_out_adobe_vec;        /* Forward decl */
  31.  
  32. static const bfd_target *aout_adobe_callback PARAMS ((bfd *));
  33.  
  34. extern boolean aout_32_slurp_symbol_table PARAMS ((bfd *abfd));
  35. extern boolean aout_32_write_syms PARAMS ((bfd *));
  36. static void aout_adobe_write_section PARAMS ((bfd *abfd, sec_ptr sect));
  37.  
  38. /* Swaps the information in an executable header taken from a raw byte
  39.    stream memory image, into the internal exec_header structure.  */
  40.  
  41. void aout_adobe_swap_exec_header_in
  42.   PARAMS ((bfd *abfd, struct external_exec *raw_bytes,
  43.        struct internal_exec *execp));
  44.      
  45. void
  46. aout_adobe_swap_exec_header_in (abfd, raw_bytes, execp)
  47.      bfd *abfd;
  48.      struct external_exec *raw_bytes;
  49.      struct internal_exec *execp;
  50. {
  51.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  52.  
  53.   /* Now fill in fields in the execp, from the bytes in the raw data.  */
  54.   execp->a_info   = bfd_h_get_32 (abfd, bytes->e_info);
  55.   execp->a_text   = GET_WORD (abfd, bytes->e_text);
  56.   execp->a_data   = GET_WORD (abfd, bytes->e_data);
  57.   execp->a_bss    = GET_WORD (abfd, bytes->e_bss);
  58.   execp->a_syms   = GET_WORD (abfd, bytes->e_syms);
  59.   execp->a_entry  = GET_WORD (abfd, bytes->e_entry);
  60.   execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
  61.   execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
  62. }
  63.  
  64. /* Swaps the information in an internal exec header structure into the
  65.    supplied buffer ready for writing to disk.  */
  66.  
  67. PROTO(void, aout_adobe_swap_exec_header_out,
  68.       (bfd *abfd,
  69.        struct internal_exec *execp,
  70.        struct external_exec *raw_bytes));
  71. void
  72. aout_adobe_swap_exec_header_out (abfd, execp, raw_bytes)
  73.      bfd *abfd;
  74.      struct internal_exec *execp;
  75.      struct external_exec *raw_bytes;
  76. {
  77.   struct external_exec *bytes = (struct external_exec *)raw_bytes;
  78.  
  79.   /* Now fill in fields in the raw data, from the fields in the exec struct. */
  80.   bfd_h_put_32 (abfd, execp->a_info  , bytes->e_info);
  81.   PUT_WORD (abfd, execp->a_text  , bytes->e_text);
  82.   PUT_WORD (abfd, execp->a_data  , bytes->e_data);
  83.   PUT_WORD (abfd, execp->a_bss   , bytes->e_bss);
  84.   PUT_WORD (abfd, execp->a_syms  , bytes->e_syms);
  85.   PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
  86.   PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
  87.   PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
  88. }
  89.  
  90.  
  91. static const bfd_target *
  92. aout_adobe_object_p (abfd)
  93.      bfd *abfd;
  94. {
  95.   struct internal_exec anexec;
  96.   struct external_exec exec_bytes;
  97.   char *targ;
  98.  
  99.   if (bfd_read ((PTR) &exec_bytes, 1, EXEC_BYTES_SIZE, abfd)
  100.       != EXEC_BYTES_SIZE) {
  101.     if (bfd_get_error () != bfd_error_system_call)
  102.       bfd_set_error (bfd_error_wrong_format);
  103.     return 0;
  104.   }
  105.  
  106.   anexec.a_info = bfd_h_get_32 (abfd, exec_bytes.e_info);
  107.  
  108.   /* Normally we just compare for the magic number.
  109.      However, a bunch of Adobe tools aren't fixed up yet; they generate
  110.      files using ZMAGIC(!).
  111.      If the environment variable GNUTARGET is set to "a.out.adobe", we will
  112.      take just about any a.out file as an Adobe a.out file.  FIXME!  */
  113.  
  114.   if (N_BADMAG (anexec)) {
  115.     extern char *getenv ();
  116.  
  117.     targ = getenv ("GNUTARGET");
  118.     if (targ && !strcmp (targ, a_out_adobe_vec.name))
  119.       ;        /* Just continue anyway, if specifically set to this format */
  120.     else
  121.       {
  122.     bfd_set_error (bfd_error_wrong_format);
  123.     return 0;
  124.       }
  125.   }
  126.  
  127.   aout_adobe_swap_exec_header_in (abfd, &exec_bytes, &anexec);
  128.   return aout_32_some_aout_object_p (abfd, &anexec, aout_adobe_callback);
  129. }
  130.  
  131.  
  132. /* Finish up the opening of a b.out file for reading.  Fill in all the
  133.    fields that are not handled by common code.  */
  134.  
  135. static const bfd_target *
  136. aout_adobe_callback (abfd)
  137.      bfd *abfd;
  138. {
  139.   struct internal_exec *execp = exec_hdr (abfd);
  140.   asection *sect;
  141.   struct external_segdesc ext[1];
  142.   char *section_name;
  143.   char try_again[30];    /* name and number */
  144.   char *newname;
  145.   int trynum;
  146.   flagword flags;
  147.  
  148.   /* Architecture and machine type -- unknown in this format.  */
  149.   bfd_set_arch_mach(abfd, bfd_arch_unknown, 0);
  150.  
  151.   /* The positions of the string table and symbol table.  */
  152.   obj_str_filepos (abfd) = N_STROFF (*execp);
  153.   obj_sym_filepos (abfd) = N_SYMOFF (*execp);
  154.  
  155.   /* Suck up the section information from the file, one section at a time.  */
  156.  
  157.   for (;;) {
  158.     if (bfd_read ((PTR) ext, 1, sizeof (*ext), abfd) != sizeof (*ext)) {
  159.       if (bfd_get_error () != bfd_error_system_call)
  160.     bfd_set_error (bfd_error_wrong_format);
  161.       return 0;
  162.     }
  163.     switch (ext->e_type[0]) {
  164.     case N_TEXT:
  165.       section_name = ".text";
  166.       flags = SEC_CODE | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  167.       break;
  168.  
  169.     case N_DATA:
  170.       section_name = ".data";
  171.       flags = SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS;
  172.       break;
  173.  
  174.     case N_BSS:
  175.       section_name = ".bss";
  176.       flags = SEC_DATA | SEC_HAS_CONTENTS;
  177.       break;
  178.  
  179.     case 0:
  180.       goto no_more_sections;
  181.  
  182.     default:
  183.       fprintf (stderr, "Unknown section type in a.out.adobe file: %x\n", 
  184.            ext->e_type[0]);
  185.       goto no_more_sections;
  186.     }
  187.  
  188.     /* First one is called ".text" or whatever; subsequent ones are
  189.        ".text1", ".text2", ... */
  190.  
  191.     bfd_set_error (bfd_error_no_error);
  192.     sect = bfd_make_section (abfd, section_name);
  193.     trynum = 0;
  194.     while (!sect) {
  195.       if (bfd_get_error () != bfd_error_no_error)
  196.     return 0;    /* Some other error -- slide into the sunset */
  197.       sprintf (try_again, "%s%d", section_name, ++trynum);
  198.       sect = bfd_make_section (abfd, try_again);
  199.     }
  200.  
  201.     /* Fix the name, if it is a sprintf'd name.  */
  202.     if (sect->name == try_again) {
  203.       newname = (char *) bfd_zalloc(abfd, strlen (sect->name));
  204.       if (newname == NULL) {
  205.     bfd_set_error (bfd_error_no_memory);
  206.     return 0;
  207.       }
  208.       strcpy (newname, sect->name);
  209.       sect->name = newname;
  210.     }
  211.  
  212.     /* Now set the section's attributes.  */
  213.     bfd_set_section_flags (abfd, sect, flags);
  214.     sect->_raw_size = ((ext->e_size[0] << 8)    /* Assumed big-endian */
  215.               | ext->e_size[1] << 8)
  216.               | ext->e_size[2];
  217.     sect->_cooked_size = sect->_raw_size;
  218.     sect->vma = bfd_h_get_32 (abfd, ext->e_virtbase);
  219.     sect->filepos = bfd_h_get_32 (abfd, ext->e_filebase);
  220.     /* FIXME XXX alignment? */
  221.  
  222.     /* Set relocation information for first section of each type.  */
  223.     if (trynum == 0) switch (ext->e_type[0]) {
  224.     case N_TEXT:
  225.       sect->rel_filepos = N_TRELOFF (*execp);
  226.       sect->reloc_count = execp->a_trsize;
  227.       break;
  228.  
  229.     case N_DATA:
  230.       sect->rel_filepos = N_DRELOFF (*execp);
  231.       sect->reloc_count = execp->a_drsize;
  232.       break;
  233.     }
  234.   }
  235. no_more_sections:  
  236.  
  237.   adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external);
  238.   adata(abfd).symbol_entry_size = sizeof (struct external_nlist);
  239.   adata(abfd).page_size = 1; /* Not applicable. */
  240.   adata(abfd).segment_size = 1; /* Not applicable. */
  241.   adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  242.  
  243.   return abfd->xvec;
  244. }
  245.  
  246. struct bout_data_struct {
  247.     struct aoutdata a;
  248.     struct internal_exec e;
  249. };
  250.  
  251. static boolean
  252. aout_adobe_mkobject (abfd)
  253.      bfd *abfd;
  254. {
  255.   struct bout_data_struct *rawptr;
  256.  
  257.   rawptr = (struct bout_data_struct *) bfd_zalloc (abfd, sizeof (struct bout_data_struct));
  258.   if (rawptr == NULL) {
  259.       bfd_set_error (bfd_error_no_memory);
  260.       return false;
  261.     }
  262.  
  263.   abfd->tdata.bout_data = rawptr;
  264.   exec_hdr (abfd) = &rawptr->e;
  265.  
  266.   adata(abfd).reloc_entry_size = sizeof (struct reloc_std_external);
  267.   adata(abfd).symbol_entry_size = sizeof (struct external_nlist);
  268.   adata(abfd).page_size = 1; /* Not applicable. */
  269.   adata(abfd).segment_size = 1; /* Not applicable. */
  270.   adata(abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  271.  
  272.   return true;
  273. }
  274.  
  275.  
  276. static boolean
  277. aout_adobe_write_object_contents (abfd)
  278.      bfd *abfd;
  279. {
  280.   struct external_exec swapped_hdr;
  281.   static struct external_segdesc sentinel[1];    /* Initialized to zero */
  282.   asection *sect;
  283.  
  284.   exec_hdr (abfd)->a_info = ZMAGIC;
  285.  
  286.   /* Calculate text size as total of text sections, etc. */
  287.  
  288.   exec_hdr (abfd)->a_text = 0;
  289.   exec_hdr (abfd)->a_data = 0;
  290.   exec_hdr (abfd)->a_bss  = 0;
  291.   exec_hdr (abfd)->a_trsize = 0;
  292.   exec_hdr (abfd)->a_drsize = 0;
  293.  
  294.   for (sect = abfd->sections; sect; sect = sect->next) {
  295.     if (sect->flags & SEC_CODE)    {
  296.       exec_hdr (abfd)->a_text += sect->_raw_size;
  297.       exec_hdr (abfd)->a_trsize += sect->reloc_count *
  298.                                    sizeof (struct reloc_std_external);
  299.     } else if (sect->flags & SEC_DATA)    {
  300.       exec_hdr (abfd)->a_data += sect->_raw_size;
  301.       exec_hdr (abfd)->a_drsize += sect->reloc_count *
  302.                                    sizeof (struct reloc_std_external);
  303.     } else if (sect->flags & SEC_ALLOC && !(sect->flags & SEC_LOAD)) {
  304.       exec_hdr (abfd)->a_bss  += sect->_raw_size;
  305.     }
  306.   }
  307.  
  308.   exec_hdr (abfd)->a_syms = bfd_get_symcount (abfd)
  309.                     * sizeof (struct external_nlist);
  310.   exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
  311.  
  312.   aout_adobe_swap_exec_header_out (abfd, exec_hdr (abfd), &swapped_hdr);
  313.  
  314.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  315.       || (bfd_write ((PTR) &swapped_hdr, 1, EXEC_BYTES_SIZE, abfd)
  316.       != EXEC_BYTES_SIZE))
  317.     return false;
  318.  
  319.   /* Now write out the section information.  Text first, data next, rest
  320.      afterward.  */
  321.  
  322.   for (sect = abfd->sections; sect; sect = sect->next) {
  323.     if (sect->flags & SEC_CODE)    {
  324.       aout_adobe_write_section (abfd, sect);
  325.     }
  326.   }
  327.   for (sect = abfd->sections; sect; sect = sect->next) {
  328.     if (sect->flags & SEC_DATA)    {
  329.       aout_adobe_write_section (abfd, sect);
  330.     }
  331.   }
  332.   for (sect = abfd->sections; sect; sect = sect->next) {
  333.     if (!(sect->flags & (SEC_CODE|SEC_DATA))) {
  334.       aout_adobe_write_section (abfd, sect);
  335.     }
  336.   }
  337.  
  338.   /* Write final `sentinel` section header (with type of 0).  */
  339.   if (bfd_write ((PTR) sentinel, 1, sizeof (*sentinel), abfd)
  340.       != sizeof (*sentinel))
  341.     return false;
  342.  
  343.   /* Now write out reloc info, followed by syms and strings */
  344.   if (bfd_get_symcount (abfd) != 0) 
  345.     {
  346.       if (bfd_seek (abfd, (file_ptr)(N_SYMOFF(*exec_hdr(abfd))), SEEK_SET)
  347.       != 0)
  348.     return false;
  349.  
  350.       if (! aout_32_write_syms (abfd))
  351.     return false;
  352.  
  353.       if (bfd_seek (abfd, (file_ptr)(N_TRELOFF(*exec_hdr(abfd))), SEEK_SET)
  354.       != 0)
  355.     return false;
  356.  
  357.       for (sect = abfd->sections; sect; sect = sect->next) {
  358.         if (sect->flags & SEC_CODE)    {
  359.           if (!aout_32_squirt_out_relocs (abfd, sect))
  360.             return false;
  361.         }
  362.       }
  363.  
  364.       if (bfd_seek (abfd, (file_ptr)(N_DRELOFF(*exec_hdr(abfd))), SEEK_SET)
  365.       != 0)
  366.     return false;
  367.  
  368.       for (sect = abfd->sections; sect; sect = sect->next) {
  369.         if (sect->flags & SEC_DATA)    {
  370.           if (!aout_32_squirt_out_relocs (abfd, sect))
  371.             return false;
  372.         }
  373.       }
  374.     }
  375.   return true;
  376. }
  377.  
  378. static void
  379. aout_adobe_write_section (abfd, sect)
  380.      bfd *abfd;
  381.      sec_ptr sect;
  382. {
  383.   /* FIXME XXX */
  384. }
  385.  
  386. static boolean
  387. aout_adobe_set_section_contents (abfd, section, location, offset, count)
  388.      bfd *abfd;
  389.      sec_ptr section;
  390.      unsigned char *location;
  391.      file_ptr offset;
  392.       int count;
  393. {
  394.   file_ptr section_start;
  395.   sec_ptr sect;
  396.  
  397.   if (abfd->output_has_begun == false) { /* set by bfd.c handler */
  398.  
  399.     /* Assign file offsets to sections.  Text sections are first, and
  400.        are contiguous.  Then data sections.  Everything else at the end.  */
  401.  
  402.     section_start = N_TXTOFF (ignore<-->me);
  403.  
  404.     for (sect = abfd->sections; sect; sect = sect->next) {
  405.       if (sect->flags & SEC_CODE)    {
  406.         sect->filepos = section_start;
  407.     /* FIXME:  Round to alignment */
  408.     section_start += sect->_raw_size;
  409.       }
  410.     }
  411.  
  412.     for (sect = abfd->sections; sect; sect = sect->next) {
  413.       if (sect->flags & SEC_DATA)    {
  414.         sect->filepos = section_start;
  415.     /* FIXME:  Round to alignment */
  416.     section_start += sect->_raw_size;
  417.       }
  418.     }
  419.  
  420.     for (sect = abfd->sections; sect; sect = sect->next) {
  421.       if (sect->flags & SEC_HAS_CONTENTS &&
  422.       !(sect->flags & (SEC_CODE|SEC_DATA)))    {
  423.         sect->filepos = section_start;
  424.     /* FIXME:  Round to alignment */
  425.     section_start += sect->_raw_size;
  426.       }
  427.     }
  428.   }
  429.  
  430.   /* regardless, once we know what we're doing, we might as well get going */
  431.   if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0)
  432.     return false;
  433.  
  434.   if (count != 0) {
  435.     return (bfd_write ((PTR)location, 1, count, abfd) == count) ?true:false;
  436.   }
  437.   return true;
  438. }
  439.  
  440. static boolean
  441. aout_adobe_set_arch_mach (abfd, arch, machine)
  442.      bfd *abfd;
  443.      enum bfd_architecture arch;
  444.      unsigned long machine;
  445. {
  446.   if (! bfd_default_set_arch_mach (abfd, arch, machine))
  447.     return false;
  448.  
  449.   if (arch == bfd_arch_unknown
  450.       || arch == bfd_arch_m68k)
  451.     return true;
  452.  
  453.   return false;
  454. }
  455.  
  456. static int 
  457. aout_adobe_sizeof_headers (ignore_abfd, ignore)
  458.      bfd *ignore_abfd;
  459.      boolean ignore;
  460. {
  461.   return sizeof(struct internal_exec);
  462. }
  463.  
  464.  
  465.  
  466.  
  467. /* Build the transfer vector for Adobe A.Out files.  */
  468.  
  469. #define aout_32_close_and_cleanup aout_32_bfd_free_cached_info
  470.  
  471. #define aout_32_bfd_make_debug_symbol \
  472.   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
  473.  
  474. #define aout_32_bfd_reloc_type_lookup \
  475.   ((CONST struct reloc_howto_struct *(*) \
  476.     PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
  477.  
  478. #define    aout_32_set_arch_mach        aout_adobe_set_arch_mach
  479. #define    aout_32_set_section_contents    aout_adobe_set_section_contents
  480.  
  481. #define    aout_32_sizeof_headers        aout_adobe_sizeof_headers
  482. #define aout_32_bfd_get_relocated_section_contents \
  483.   bfd_generic_get_relocated_section_contents
  484. #define aout_32_bfd_relax_section       bfd_generic_relax_section
  485. #define aout_32_bfd_link_hash_table_create \
  486.   _bfd_generic_link_hash_table_create
  487. #define aout_32_bfd_link_add_symbols    _bfd_generic_link_add_symbols
  488. #define aout_32_bfd_final_link        _bfd_generic_final_link
  489.  
  490. const bfd_target a_out_adobe_vec =
  491. {
  492.   "a.out.adobe",        /* name */
  493.   bfd_target_aout_flavour,
  494.   true,                /* data byte order is unknown (big assumed) */
  495.   true,                /* hdr byte order is big */
  496.   (HAS_RELOC | EXEC_P |        /* object flags */
  497.    HAS_LINENO | HAS_DEBUG |
  498.    HAS_SYMS | HAS_LOCALS | WP_TEXT ),
  499.   /* section flags */
  500.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_DATA | SEC_RELOC),
  501.   '_',                /*  symbol leading char */
  502.   ' ',                /* ar_pad_char */
  503.   16,                /* ar_max_namelen */
  504.   2,                /* minumum alignment power */
  505.  
  506.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  507.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  508.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  509.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  510.      bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  511.      bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  512.  {_bfd_dummy_target, aout_adobe_object_p, /* bfd_check_format */
  513.    bfd_generic_archive_p, _bfd_dummy_target},
  514.  {bfd_false, aout_adobe_mkobject, /* bfd_set_format */
  515.    _bfd_generic_mkarchive, bfd_false},
  516.  {bfd_false, aout_adobe_write_object_contents, /* bfd_write_contents */
  517.    _bfd_write_archive_contents, bfd_false},
  518.  
  519.      BFD_JUMP_TABLE_GENERIC (aout_32),
  520.      BFD_JUMP_TABLE_COPY (_bfd_generic),
  521.      BFD_JUMP_TABLE_CORE (_bfd_nocore),
  522.      BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_bsd),
  523.      BFD_JUMP_TABLE_SYMBOLS (aout_32),
  524.      BFD_JUMP_TABLE_RELOCS (aout_32),
  525.      BFD_JUMP_TABLE_WRITE (aout_32),
  526.      BFD_JUMP_TABLE_LINK (aout_32),
  527.      BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  528.  
  529.   (PTR) 0
  530. };
  531.